<?php
/**
 * Pertyler DatabaseManagement
 * Control
 * Table
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once __DIR__ . '/main.php';

if(isset($_POST['Select']))
	return require __DIR__ . '/SearchedRowArray.php';

if(isset($_POST['SelectUnpublished']))
	return require __DIR__ . '/SearchedUnpublishedRowArray.php';

if(isset($_POST['Insert']))
	return require __DIR__ . '/InsertRow.php';

if(!isset($_GET['Table']))
	throw new \Exception('Table parameter required.');

require_once __DIR__ . '/../databaseModel/Role.php';

if(!($moderator = Role_Moderator::Select()))
	return require __DIR__ . '/Index.php';

require_once __DIR__ . '/../databaseModel/Table.php';

if(!($table = Table::Select($_GET['Table'])))
	throw new \Exception('Table cannot found.');

if(!$table->SelectPrivilege('select'))
	throw new \Exception('Table does not have select privilege.');

if(!Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select')))
	throw new \Exception('Select privilege required.');

require_once __DIR__ . '/../library/Pert/General/Page.php';

\Pert\Page::Get()->AddTitle($table);

$Content = \Pert\Page::Get()->Content();

$Content->TableName = (string) $table;

$stringColumnArray = array();

$stringColumnValueArray = array();

$referancedColumnArray = array();

$referancedColumnTableIdArray = array();

$referancedColumnValueArrayArray = array();

$selectedReferancedColumnValueArrayArray = array();

foreach($table->SelectColumnArray() AS $column)
	if($column->String())
	{
		$stringColumnArray[$column->Priority()] = (string) $column;

		if(isset($_POST[$column->Priority()]))
			if($_POST[$column->Priority()])
				$stringColumnValueArray[$column->Priority()] = $_POST[$column->Priority()];
	}
	elseif($column->RelatedTable())
	{
		$referancedColumnArray[$column->Priority()] = (string) $column;

		$referancedColumnTableIdArray[$column->Priority()] = $column->RelatedTable()->Id();

		foreach($column->RelatedTable()->SelectPublishedRowArray() AS $publishedRow)
			$referancedColumnValueArrayArray[$column->Priority()][$publishedRow->Id()] = (string) $publishedRow;

		if(isset($_POST[$column->Priority()]))
			if($_POST[$column->Priority()])
				foreach($_POST[$column->Priority()] AS $id)
					$selectedReferancedColumnValueArrayArray[$column->Priority()][$id] = true;
	}

$Content->StringColumnArray = $stringColumnArray;

unset($stringColumnArray);

$Content->StringColumnValueArray = $stringColumnValueArray;

unset($stringColumnValueArray);

$Content->ReferancedColumnArray = $referancedColumnArray;

unset($referancedColumnArray);

$Content->ReferancedColumnTableIdArray = $referancedColumnTableIdArray;

unset($referancedColumnTableIdArray);

$Content->ReferancedColumnValueArrayArray = $referancedColumnValueArrayArray;

unset($referancedColumnValueArrayArray);

$Content->SelectedReferancedColumnValueArrayArray = $selectedReferancedColumnValueArrayArray;

unset($selectedReferancedColumnValueArrayArray);

if($table->HasUnpublishedColumn())
	$Content->UnpublishedColumn = true;

if($table->SelectPrivilege('approve'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('approve')))
		$Content->ApprovePrivilege = true;

if($table->SelectPrivilege('update'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update')))
		$Content->UpdatePrivilege = true;

if($table->SelectPrivilege('insert'))
	if(Role::Select()->SelectRolePrivilege($table->SelectPrivilege('insert')))
		$Content->InsertPrivilege = true;

unset($table);

unset($_GET);

unset($_POST);

echo \Pert\Page::Get()->HeaderHTML();

require __DIR__ . '/../view/Table.php';

echo \Pert\Page::Get()->FooterHTML();

return true;
